package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.zhongyichou.adapter.DreamFundraisingAdapter;
import com.example.administrator.zhongyichou.adapter.IllnessFundraisingAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.DiseaseListProtocol;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/24.
 * 描述：梦想筹款
 */

public class DreamFundraisingActivity extends BaseActivity<DiseaseListProtocol> implements AdapterView.OnItemClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private IllnessFundraisingAdapter adapter;
    private int page = 1;
    private int pageSize = 10;
    private DiseaseListProtocol protocol;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        listview.setOnItemClickListener(this);
        refresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (page == protocol.getTotal_page()) {
                    refresh.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showToast("已经没有更多数据了");
                            refresh.finishLoadmore();
                            return;
                        }
                    }, 1000);
                }
                page++;
                addData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                requestData();
            }
        });
    }

    private void addData() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(DiseaseListProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("page_size", pageSize + "");
        requestHttpLoader.setParams(NetConfig.DISEASE_LIST, map);
        requestHttpLoader.executor(new ResultListener<DiseaseListProtocol>() {
            @Override
            public void success(DiseaseListProtocol mGsonBaseProtocol) {
                refresh.finishLoadmore();
                protocol.getData().addAll(mGsonBaseProtocol.getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    @Override
    protected void responseData(DiseaseListProtocol mGsonBaseProtocal) {
        refresh.finishRefresh();
        protocol = mGsonBaseProtocal;
        adapter = new IllnessFundraisingAdapter(this, protocol.getData());
        listview.setAdapter(adapter);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(DiseaseListProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("page_size", pageSize + "");
        requestHttpLoader.setParams(NetConfig.DREAM_LIST, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.illness_fundraising_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("梦想筹款");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, DreamDetailsActivity.class);
        intent.putExtra("info_id", protocol.getData().get(position).getInfo_id());
        startActivity(intent);
    }

}
